<template>
  <el-dialog
    :title="!this.dataForm.stId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">
    <el-form-item label="分类名" prop="stName">
      <el-input v-model="dataForm.stName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="stImg">
      <singleupload v-model="dataForm.stImg"></singleupload>
      <!-- <el-input v-model="dataForm.stImg" placeholder=""></el-input> -->
    </el-form-item>
    <el-form-item label="热门" prop="stBadge">
       <el-select v-model="dataForm.stBadge" placeholder="请选择">
          <el-option
            v-for="item in rms"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      <!-- <el-input v-model="dataForm.stBadge" placeholder=""></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleupload from "@/components/upload/singleUpload.vue";
    import api from "@/api/modules/cbbsort";
  export default {
    components:{
       singleupload
    },
    data () {
      return {
        rms: [
        {
          value: "热门", //0未支付,1进行中，2已完成，-1支付取消
          label: "热门",
        },
        {
          value: " ", //0未支付,1进行中，2已完成，-1支付取消
          label: "非热门",
        },
      ],
        visible: false,
        dataForm: {
          stId: 0,
          stName: '',
          stImg: '',
          stBadge: '',
          stBackgroundImage: '',
          stProductSpecifications: ''
        },
        dataRule: {
          stName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stImg: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stBadge: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stBackgroundImage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stProductSpecifications: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.stId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          api.info(this.dataForm.stId).then(({data}) => {
              if (data) {
                this.dataForm.stName = data.cbbSort.stName
                this.dataForm.stImg = data.cbbSort.stImg
                this.dataForm.stBadge = data.cbbSort.stBadge
                this.dataForm.stBackgroundImage = data.cbbSort.stBackgroundImage
                this.dataForm.stProductSpecifications = data.cbbSort.stProductSpecifications
              }
            })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
    var parms= {
'stId': this.dataForm.stId || undefined,
'stName': this.dataForm.stName,
'stImg': this.dataForm.stImg,
'stBadge': this.dataForm.stBadge,
'stBackgroundImage': this.dataForm.stBackgroundImage,
'stProductSpecifications': this.dataForm.stProductSpecifications        }
        api.updateAdd(parms,this.dataForm.stId).then((res) => {
              if (res.code==200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
